<template>
  <el-dialog
    title="回复"
    :visible.sync="visible"
    width="40%"
    v-if="visible"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="内容" prop="content">
        <el-input type="textarea" :rows="10" v-model="form.content"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleReply">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getMailboxDetailApi, mailboxReplyInsertApi, updateReplyApi } from '@/api/modules/approve.api'
export default {
  data() {
    return {
      visible: false,
      type: 0,
      updateId: '',
      form: {
        mailId: "",
        content: "",
      },
      rules: {
        content: [
          { required: true, message: "请输入回复内容", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async init(id,type) {
      this.type = type
      if(type === 1){
        // const reply = await getMailReplyApi(id)
        const res = await getMailboxDetailApi(id);
        this.form.content = res.reply?.content
        this.updateId = res.reply?.id
      }
      this.form.mailId = id;
      this.visible = true;
    },
    handleClose() {
      this.form = {
        mailId: "",
        content: "",
      };
      this.visible = false;
    },
    async handleReply() {
      if(this.type === 1){
        await updateReplyApi({
          id: this.updateId,
          content: this.form.content
        })
      }else{
        await mailboxReplyInsertApi({
          mailId: this.form.mailId,
          content: this.form.content
        })
      }

      this.$emit("getTableData",{});
      this.visible = false;
    },
  },
};
</script>
<style scoped>
.select-width {
  width: 100%;
}
</style>
